package Demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-09-03
 * Time: 15:53
 */
public class Test {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    //二叉搜索树的特性:以中序遍历遍历该数，得到的是一个单调递增的数列每遍历到一个节点，判断该节点是否大于前一个结点值
    public TreeNode prev;
    public boolean isValidBST(TreeNode root) {
        //结束条件
        if(root == null) return true;
        //遍历左树节点
        boolean left = isValidBST(root.left);

        //处理中间节点
        if(prev == null){
            prev = root;
        }else{
            if(prev.val >= root.val){
                return false;
            }else{
                prev = root;
            }
        }
        //遍历右树节点
        boolean right = isValidBST(root.right);
        return left && right;
    }

    public static void main(String[] args) {

    }
}
